<?php

namespace app\api\controller;

use think\Controller;
use think\Cache;
use think\Db;

class Digitalcoin extends Common
{

	//我的数字币钱包
	public function index()
	{
		$setting = $this->setting;
		$data['poundage'] = $setting['sell_fee'];
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
	}

	// 行情提取
	public function price($type)
	{
		foreach (moneyprice() as $k => $v) {
			if ($v['type'] == $type) {
				return $v['today_price'];
			}
		}
	}

	// 获取私钥
	public function privatekey()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		input('secpwd') ? input('secpwd') : apifasong(['zt' => -1, 'msg' => '缺少参数']);
		if ($userinfo['secpwd'] <> MD5($appdata['secpwd'])) apifasong(['zt' => -1, 'msg' => "支付密码错误"]);

		$privatekey = db('wallet')->where(array('uid' => $userinfo['id']))->value('privatekey');

		if (!$privatekey) {
			$privatekey = '';
		}
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $privatekey]);
	}


	//二维码
	public function code()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$contract['img'] = 'http://' . $_SERVER['SERVER_NAME'] . '/' . getcode($userinfo['uname']);
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $contract]);
	}



	// 转账
	public function transfer()
	{
		apifasong(['zt' => -1, 'msg' => '功能暂时关闭']);
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;

		$params = array();
		$poundage = input('poundage');
		$money = input('money');
		$to = input('to');
		$secpwd = input('secpwd');

		if (!$money) apifasong(['zt' => -1, 'msg' => '请输入金额']);
		if (!preg_match('#^1[2,3,4,5,6,7,8,9]{1}[\d]{9}$#', $to)) apifasong(['zt' => -1, 'msg' => '请输入正确的手机号']);
		if (MD5($appdata['secpwd']) != $userinfo['secpwd']) apifasong(['zt' => -1, 'msg' => '支付密码错误']);


		if ($userinfo['uname'] == $to) apifasong(['zt' => -1, 'msg' => '不能自己转自己，不合算的买卖哦']);
		if ($userinfo['idc'] < ($money + $poundage)) apifasong(['zt' => -1, 'msg' => '帐号余额不足']);
		$user_oid = db('user')->field('id, uname, idc')->where('uname', $to)->find();
		if (!$user_oid) apifasong(['zt' => -1, 'msg' => '该用户未注册，转账失败']);

		// 启动事务
		Db::startTrans();
		try {

			db('user')->where('id', $userinfo['id'])->setDec('idc', ($money + $poundage));
			db('user')->where('uname', $to)->setInc('idc', $money);

			// 数字转账记录
			$digital_order["uid_uname"] = $userinfo['uname']; // 用户名
			$digital_order["uid"] = $userinfo['id'];
			$digital_order["oid_uname"] = $user_oid['uname']; //
			$digital_order["oid"] = $user_oid['id']; //
			$digital_order["uid_money"] = '-' . $money; //变动数量
			$digital_order["oid_money"] = '+' . $money; //变动数量
			$digital_order["oid_poundage"] = $poundage; //手续费
			$digital_order["time"] = time();

			$order_id = db('digital_order')->insertGetId($digital_order);


			// 财务记录转出
			$logocoin_out["uname"] = $userinfo['uname']; // 用户名
			$logocoin_out["uid"] = $userinfo['id']; // 金幣
			$logocoin_out["type"] = 7;
			$logocoin_out["mqian"] = $userinfo['idc']; //当前帐户金币余额
			$logocoin_out["money"] = '-' . $money; //变动数量
			$logocoin_out["mhou"] = $userinfo['idc'] - $money; //变动之后的
			$logocoin_out["note"] = '转出IDC';
			$logocoin_out["oid"] = $user_oid['id'];
			$logocoin_out["oname"] = $user_oid['uname'];
			$logocoin_out["order_id"] = $order_id;
			$logocoin_out["time"] = time();
			db('logocoin')->insert($logocoin_out);


			// 财务记录转入
			$logocoin_enter["uname"] = $user_oid['uname']; // 用户名
			$logocoin_enter["uid"] = $user_oid['id']; // 金幣
			$logocoin_enter["type"] = 8;
			$logocoin_enter["mqian"] = $user_oid['idc']; //当前帐户金币余额
			$logocoin_enter["money"] = $money; //变动数量
			$logocoin_enter["mhou"] = $user_oid['idc'] + $money; //变动之后的
			$logocoin_enter["note"] = '转入IDC';
			$logocoin_enter["oid"] = $userinfo['id'];
			$logocoin_enter["oname"] = $userinfo['uname'];
			$logocoin_enter["order_id"] = $order_id;
			$logocoin_enter["time"] = time();

			db('logocoin')->insert($logocoin_enter);
			// dump($a);die;

			// 提交事务
			Db::commit();
			$userdata = db('user')->field(true)->where(['uname' => $userinfo['uname']])->find();
			apifasong(['zt' => 1, 'msg' => '转账成功', 'userinfo' => apiuserbaseinfo($userdata)]);
		} catch (\Throwable $th) {
			// 回滚事务
			Db::rollback();
			apifasong(['zt' => -1, 'msg' => '转账失败']);
		}
	}

	// 订单
	public function order()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$data = array();
		$page = input('page') ? input('page')  + 1 : 0;
		$user_order = db('digital_order')->field(true)->where('uid', $userinfo['id'])->whereor('oid', $userinfo['id'])->order('id desc')->page($page, 15)->select();

		foreach ($user_order as $k => $v) {
			if ($v['uid'] == $userinfo['id']) {
				$data[$k]['uname'] = $v['oid_uname'];
				$data[$k]['money'] = $v['uid_money'];
				$data[$k]['note'] = $v['uid_note'];
				$data[$k]['poundage'] = $v['oid_poundage'];
				$data[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
			}elseif ($v['oid'] == $userinfo['id']) {
				$data[$k]['uname'] = $v['uid_uname'];
				$data[$k]['money'] = $v['oid_money'];
				$data[$k]['note'] = $v['oid_note'];
				$data[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
			} 
		}

		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
	}
}
